public class Sort {

    /**
     * 直接插入排序
     * 时间复杂度:O(N^2)
     * 空间复杂度:O(1)
     * 稳定性:稳定
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 希尔排序(对插入排序进行的优化)
     * 时间复杂度:O(N^1.3~N^1.5)
     * 空间复杂度:O(1)
     * 稳定性:不稳定
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap >1) {
            gap /= 2;
            shell(array,gap);
        }
        shell(array,gap);
    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    array[j+gap] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 选择排序
     * 时间复杂度:O(N^2)
     * 空间复杂度:O(1)
     * 稳定性:不稳定
     * @param array
     */
    public static void selectionSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    private static void swap(int[] array,int a,int b) {
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }


    /**
     * 堆排序
     * 时间复杂度:O(N*log2^N)
     * 空间复杂度:O(1)
     * 稳定性:不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length);
        }
    }

    private static void shiftDown(int[] array,int parent,int len) {
        int child = parent*2+1;
        while (child < len ) {
            if (child+1 < len && array[child] < array[child+1]) {
                child = child + 1;
            }
            if (array[child] > array[parent]) {
                    swap(array,child,parent);
                    parent = child;
                    child = 2*parent+1;
            }else {
                break;
            }
        }
    }


    /**
     * 冒泡排序
     * 时间复杂度:O(N^2)
     * 空间复杂度:O(1)
     * 稳定性:稳定
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if (flag == false) {
                return;
            }
        }
    }

    /**
     * 快速排序
     * 时间复杂度:O(N*log2^N)
     * 空间复杂度:O(1)
     * 稳定性:不稳定
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left<right && array[right]>=tmp) {
                right--;
            }
            array[left] = array[right];
            while (left<right && array[left]<=tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }


    /**
     * 归并排序
     * 时间复杂度:O(N*log2^N)
     * 空间复杂度:O(N)
     * 稳定性:稳定
     */
    public static void mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }

    private static void mergeSortFunc(int[] array,int left,int right) {
        if (left >= right) {
            return;
        }
        int mid = (left+right)/2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }

    private static void merge(int[] array,int start,int end,int mid) {
        int s1 = start;
        int s2 = mid+1;
        int k = 0;
        int[] arr = new int[end-start+1];
        while (s1 <= mid && s2 <= end) {
            if (array[s1] >= array[s2]) {
                arr[k] = array[s2];
                k++;
                s2++;
            }else {
                arr[k] = array[s1];
                k++;
                s1++;
            }
        }
        while (s1 <= mid) {
            arr[k] = array[s1];
            k++;
            s1++;
        }
        while (s2 <= end) {
            arr[k] = array[s2];
            k++;
            s2++;
        }
        for (int i = 0; i < arr.length; i++) {
            array[i+start] = arr[i];
        }
    }
}
